﻿using MySql.Data.MySqlClient;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Runtime.Serialization;
using System.Text;
using System.Threading.Tasks;
using utils;

namespace dal.cgm_production_dao
{
    [DataContract]
    public class BluetoothTestSensorInfo
    {
        [DataMember]
        public int id { get; set; }
        [DataMember]
        public int code_id { get; set; }
        [DataMember]
        public string cover_code { get; set; }
        [DataMember]
        public string mac_address { get; set; }
        [DataMember]
        public DateTime create_time { get; set; }
        [DataMember]
        public DateTime update_time { get; set; }
        [DataMember]
        public int print_count { get; set; }
    }

    public class BluetoothTestSensorInfoDao
    {
        public static void Insert(BluetoothTestSensorInfo data)
        {
            string sql = @"INSERT INTO bluetooth_test_sensor_info
           (code_id,cover_code,mac_address,print_count)
           VALUES
           (@code_id,@cover_code,@mac_address,@count)";
            List<MySqlParameter> parameters = new List<MySqlParameter>();
            parameters.Add(new MySqlParameter("@code_id", data.code_id));
            parameters.Add(new MySqlParameter("@cover_code", data.cover_code));
            parameters.Add(new MySqlParameter("@mac_address", data.mac_address));
            parameters.Add(new MySqlParameter("@count", data.print_count));
            MySqlHelper.ExecuteNonQuery(ConfigString.cgm_production, sql, parameters.ToArray());
        }

        public static BluetoothTestSensorInfo GetMac(string mac)
        {
            string sql = @"SELECT id ,code_id,cover_code,mac_address,create_time,update_time,print_count FROM bluetooth_test_sensor_info WHERE mac_address=@mac_address";
            List<MySqlParameter> parameters = new List<MySqlParameter>();
            parameters.Add(new MySqlParameter("@mac_address", mac));
            DataSet ds = MySqlHelper.ExecuteDataset(ConfigString.cgm_production, sql, parameters.ToArray());

            IList<BluetoothTestSensorInfo> list = DataContainerUtil.DataTableToList<BluetoothTestSensorInfo>(ds.Tables[0]);
            if (list.Count > 0)
            {
                return list[0];
            }
            else
            {
                return new BluetoothTestSensorInfo();
            }
        }

        public static BluetoothTestSensorInfo GetCoverCode(string cover_code)
        {
            string sql = @"SELECT id,code_id,cover_code,mac_address,create_time,update_time,print_count FROM bluetooth_test_sensor_info WHERE cover_code=@cover_code";
            List<MySqlParameter> parameters = new List<MySqlParameter>();
            parameters.Add(new MySqlParameter("@cover_code", cover_code));
            DataSet ds = MySqlHelper.ExecuteDataset(ConfigString.cgm_production, sql, parameters.ToArray());

            IList<BluetoothTestSensorInfo> list = DataContainerUtil.DataTableToList<BluetoothTestSensorInfo>(ds.Tables[0]);
            if (list.Count > 0)
            {
                return list[0];
            }
            else
            {
                return new BluetoothTestSensorInfo();
            }
        }

        public static BluetoothTestSensorInfo GetMacByDmCode(string cover_code)
        {
            string sql = @"SELECT id,code_id,cover_code,mac_address,create_time,update_time,print_count FROM bluetooth_test_sensor_info WHERE cover_code = @cover_code";
            List<MySqlParameter> parameters = new List<MySqlParameter>();
            parameters.Add(new MySqlParameter("@cover_code", cover_code));
            DataSet ds = MySqlHelper.ExecuteDataset(ConfigString.cgm_production, sql, parameters.ToArray());

            IList<BluetoothTestSensorInfo> list = DataContainerUtil.DataTableToList<BluetoothTestSensorInfo>(ds.Tables[0]);
            if (list.Count > 0)
            {
                return list[0];
            }
            else
            {
                return new BluetoothTestSensorInfo();
            }
        }


        public static void UpDate(BluetoothTestSensorInfo info)
        {
            if (info != null)
            {
                List<MySqlParameter> parameters = new List<MySqlParameter>();
                string sql = " UPDATE bluetooth_test_sensor_info SET";
                sql = sql + " print_count=@print_count,";
                parameters.Add(new MySqlParameter("@print_count", info.print_count));
                sql = sql + " update_time=@update_time";
                parameters.Add(new MySqlParameter("@update_time", info.update_time));
                sql = sql + " WHERE cover_code=@cover_code";
                parameters.Add(new MySqlParameter("@cover_code", info.cover_code));
                sql = sql + " and  mac_address=@mac_address";
                parameters.Add(new MySqlParameter("@mac_address", info.mac_address));

                MySqlHelper.ExecuteDataset(ConfigString.cgm_production, sql, parameters.ToArray());
            }
        }
    }
}
